package config

import (
	log "github.com/sirupsen/logrus"
	"strconv"
	"strings"
)

func Init() {
	log.Println("初始化系统配置")

	cfg, err := newConfigure()
	if err == nil {

		log.Println("配置日志设置中")

		if cfg.HasSection(LOG) {
			if cfg.HasOption(LOG, OPEN) {
				v, err := cfg.RawString(LOG, OPEN)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						LOG_OPEN = true
					}
				}
			}

			if LOG_OPEN && cfg.HasOption(LOG, PATH) {
				v, err := cfg.RawString(LOG, PATH)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						LOG_PATH = strings.TrimSpace(v)
					}
				}
			}

			if LOG_OPEN && cfg.HasOption(LOG, CONSOLE) {
				v, err := cfg.RawString(LOG, CONSOLE)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						LOG_CONSOLE = true
					}
				}
			}

		}

		log.Println("配置TCP设置中")

		if cfg.HasSection(TCP) {

			if cfg.HasOption(TCP, START) {
				v, err := cfg.RawString(TCP, START)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						TCP_START = true
					}
				}
			}

			if TCP_START && cfg.HasOption(TCP, PORT) {
				v, err := cfg.RawString(TCP, PORT)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						p_, err := strconv.Atoi(strings.TrimSpace(v))
						if err == nil {
							TCP_PORT = p_
							log.Println("TCP端口", TCP_PORT)
						}
					}
				}
			}

		}

		log.Println("配置websocket设置中")

		if cfg.HasSection(WS) {

			if cfg.HasOption(TCP, START) {
				v, err := cfg.RawString(WS, START)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						WS_START = true
					}
				}
			}
		}

		log.Println("配置HTTP设置中")

		if cfg.HasSection(HTTP) {

			if cfg.HasOption(HTTP, START) {
				v, err := cfg.RawString(HTTP, START)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						HTTP_START = true
					}
				}
			}

			if cfg.HasOption(HTTP, SSL) {
				v, err := cfg.RawString(HTTP, SSL)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						HTTP_SSL = true
					}
				}
			}

			if HTTP_START && cfg.HasOption(HTTP, PORT) {
				v, err := cfg.RawString(HTTP, PORT)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						port, err := strconv.Atoi(strings.TrimSpace(v))
						if err == nil {
							HTTP_PORT = port
							log.Println("HTTP端口", HTTP_PORT)
						}
					}
				}
			}

			if HTTP_START && cfg.HasOption(HTTP, CRTNAME) {
				v, err := cfg.RawString(HTTP, CRTNAME)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						CRT_NAME = strings.TrimSpace(v)
					}
				}
			}
			if HTTP_START && cfg.HasOption(HTTP, KEYNAME) {
				v, err := cfg.RawString(HTTP, KEYNAME)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						KEY_NAME = strings.TrimSpace(v)
					}
				}
			}
		}

		log.Println("配置HTTP消息引擎设置中")

		if cfg.HasSection(EX) {

			if cfg.HasOption(EX, START) {
				v, err := cfg.RawString(EX, START)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						EX_START = true
					} else {
						EX_START = false
					}
				}
			}

			if EX_START && cfg.HasOption(EX, DEADLINE) {
				v, err := cfg.RawString(EX, DEADLINE)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						deadline, err := strconv.Atoi(strings.TrimSpace(v))
						if err == nil {
							EX_DEADLINE = deadline
						}
					}
				}
			}

			if EX_START && cfg.HasOption(EX, LIVETIME) {
				v, err := cfg.RawString(EX, LIVETIME)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						livetime, err := strconv.Atoi(strings.TrimSpace(v))
						if err == nil {
							EX_LIVETIME = livetime
						}
					}
				}
			}

			if EX_START && cfg.HasOption(EX, MODEL) {
				v, err := cfg.RawString(EX, MODEL)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						EX_MODEL = strings.TrimSpace(v)
						log.Println("EX运行模式", EX_MODEL)
					}
				}
			}
		}
		log.Println("配置集群设置中")

		if cfg.HasSection(CLUSTER) {

			if cfg.HasOption(CLUSTER, START) {
				v, err := cfg.RawString(CLUSTER, START)
				if err == nil {
					if strings.TrimSpace(v) == "true" {
						CLUSTER_START = true
					} else {
						CLUSTER_START = false
					}
				}
			}

			if CLUSTER_START && cfg.HasOption(CLUSTER, LOCAL_PORT) {
				v, err := cfg.RawString(CLUSTER, LOCAL_PORT)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						localPort, err := strconv.Atoi(strings.TrimSpace(v))
						if err == nil {
							CLUSTER_LOCAL_PORT = localPort
							log.Println("集群本地端口", CLUSTER_LOCAL_PORT)
						}
					}
				}
			}

			if CLUSTER_START && cfg.HasOption(CLUSTER, REMOTE_PORT) {
				v, err := cfg.RawString(CLUSTER, REMOTE_PORT)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						remotePort, err := strconv.Atoi(strings.TrimSpace(v))
						if err == nil {
							CLUSTER_REMOTE_PORT = remotePort
							log.Println("集群远程端口", CLUSTER_REMOTE_PORT)
						}
					}
				}
			}

			if CLUSTER_START && cfg.HasOption(CLUSTER, REMOTE_HOST) {
				v, err := cfg.RawString(CLUSTER, REMOTE_HOST)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						CLUSTER_REMOTE_HOST = strings.TrimSpace(v)
						log.Println("集群远程地址", CLUSTER_REMOTE_HOST)
					}
				}
			}


			if CLUSTER_START && cfg.HasOption(CLUSTER, REAL_POST) {
				v, err := cfg.RawString(CLUSTER, REAL_POST)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						realPort, err := strconv.Atoi(strings.TrimSpace(v))
						if err == nil {
							CLUSTER_REAL_PORT = realPort
							log.Println("集群外部端口", CLUSTER_REAL_PORT)
						}
					}
				}
			}

			if CLUSTER_START && cfg.HasOption(CLUSTER, REAL_HOST) {
				v, err := cfg.RawString(CLUSTER, REAL_HOST)
				if err == nil {
					if len(strings.TrimSpace(v)) > 0 {
						CLUSTER_REAL_HOST = strings.TrimSpace(v)
						log.Println("集群外部地址", CLUSTER_REAL_HOST)
					}
				}
			}

		}
	} else {
		log.Panicln("读取系统配置失败", err)
	}
}

/**
func (cfg *Configure) GetValue(key string) string {

	if cfg.Cg.HasSection(cfg.section) {
		if cfg.Cg.HasOption(cfg.section, key) {
			v, err := cfg.Cg.RawString(cfg.section, key)
			if err == nil {
				v := strings.Trim(v, " ")
				return v

			}

		}
	}
	return ""
}
*/
